<template>
  <div class="order-stats-chart">
    <t-loading :loading="loading">
      <div ref="chartRef" class="chart-container"></div>
      <div v-if="!loading && !data.length" class="empty-data">
        <t-empty description="暂无数据" />
      </div>
    </t-loading>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch } from 'vue'
import type { OrderStatData } from '../../../api/statistics'
import * as echarts from 'echarts/core'
import { BarChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent
} from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'

// 注册必需的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  CanvasRenderer
])

const props = defineProps<{
  data: OrderStatData[]
  loading: boolean
}>()

const chartRef = ref<HTMLElement | null>(null)
const chart = ref<echarts.ECharts | null>(null)

// 初始化图表
onMounted(() => {
  if (chartRef.value) {
    chart.value = echarts.init(chartRef.value)
    updateChart()
    
    // 监听窗口大小变化
    window.addEventListener('resize', handleResize)
  }
})

// 组件卸载时清理
onUnmounted(() => {
  if (chart.value) {
    chart.value.dispose()
  }
  window.removeEventListener('resize', handleResize)
})

// 监听数据变化
watch(() => props.data, updateChart, { deep: true })

// 处理窗口大小变化
const handleResize = () => {
  if (chart.value) {
    chart.value.resize()
  }
}

// 更新图表
const updateChart = () => {
  if (!chart.value || !props.data.length) return
  
  const xAxisData = props.data.map(item => item.date)
  const countData = props.data.map(item => item.count)
  const amountData = props.data.map(item => item.amount)
  
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: (params: any) => {
        const countData = params[0]
        const amountData = params[1]
        return `${countData.name}<br/>${countData.marker}订单数：${countData.value}<br/>${amountData.marker}金额：¥${amountData.value}`
      }
    },
    legend: {
      data: ['订单数', '订单金额'],
      right: 10,
      top: 0
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: xAxisData,
      axisLine: {
        lineStyle: {
          color: '#E6E6E6'
        }
      },
      axisLabel: {
        color: '#666'
      }
    },
    yAxis: [
      {
        type: 'value',
        name: '订单数',
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        splitLine: {
          lineStyle: {
            color: '#E6E6E6',
            type: 'dashed'
          }
        },
        axisLabel: {
          color: '#666'
        }
      },
      {
        type: 'value',
        name: '金额(元)',
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        splitLine: {
          show: false
        },
        axisLabel: {
          color: '#666',
          formatter: (value: number) => {
            return `¥${value}`
          }
        }
      }
    ],
    series: [
      {
        name: '订单数',
        type: 'bar',
        data: countData,
        barWidth: 12,
        itemStyle: {
          color: '#0052D9'
        }
      },
      {
        name: '订单金额',
        type: 'bar',
        yAxisIndex: 1,
        data: amountData,
        barWidth: 12,
        itemStyle: {
          color: '#00A870'
        }
      }
    ]
  }
  
  chart.value.setOption(option)
}
</script>

<style scoped>
.order-stats-chart {
  width: 100%;
  height: 100%;
  position: relative;
}

.chart-container {
  width: 100%;
  height: 350px;
}

.empty-data {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style> 